<?php

namespace app\adminapi\controller;
use \app\common\business\Login as loginBs;
use think\exception\ValidateException;
use think\facade\Cache;


class Login extends BaseApi
{
    /**
     * 验证码接口
     */
    public function captcha()
    {
        $result = (new loginBs())->getCaptcha();
        $this->ok($result);

    }

    public function index()
    {
        //接收参数
        $params = input();

        //数据效验
        try {
            validate(\app\adminapi\validate\Login::class)->check($params);
        }catch (ValidateException $e){
            $this->fail($e->getMessage());
        }
        //登录效验

        try{
            $result = (new loginBs())->login($params);
        }catch(\Exception $e){
            $this->fail($e->getMessage());
        }
        $this->ok($result);

    }

    public function logout()
    {
        //退出登录  清空token
        $token = $_SERVER['HTTP_AUTHORIZATION'];
        $tokenObjPath = \ClassAttr::getTokenClass();
        $jwtObj = \ClassAttr::initClass('jwtToken',$tokenObjPath);
        $result = $jwtObj::checkToken($token);
        $userData = (array)$result['data'];
        $userName = $userData['username'];
        Cache::store('redisLogin')->delete(config('redis.token').$userName);
        $this->ok();

    }

}